package com.furongji.education.controller.admin;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.furongji.common.result.Result;
import com.furongji.education.domain.po.Subject;
import com.furongji.education.domain.vo.SubjectVo;
import com.furongji.education.service.SubjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author furongji
 * @since 2025-05-18
 */
@Tag(name = "课程管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/courses")
public class SubjectAdminController {

    private final SubjectService subjectService;
    private final ServiceRegistryAutoConfiguration serviceRegistryAutoConfiguration;

    @Operation(summary = "查询课程分类")
    @GetMapping("/listSubject")
    public Result listSubject() {
        List<SubjectVo> subjectVoList = subjectService.selectSubject();
        return Result.success(subjectVoList);
    }

    @Operation(summary = "添加课程分类")
    @PostMapping("/addSubject")
    public Result addSubject(@RequestBody Map<String, String> map) {
        String name = map.get("name");
        String parentId = map.get("parentId");
        subjectService.addSubject(name, parentId);
        return Result.success();
    }

    @Operation(summary = "删除课程分类")
    @DeleteMapping("/deleteSubject/{id}")
    public Result deleteSubject(@PathVariable String id) {
        subjectService.deleteBySubjectId(id);
        return Result.success();
    }

    @Operation(summary = "修改课程分类")
    @PutMapping("/updateSubject")
    public Result updateSubject(@RequestBody SubjectVo subjectVo) {
        String id = subjectVo.getId();
        String name = subjectVo.getName();
        LambdaUpdateWrapper<Subject> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Subject::getId, id);
        wrapper.set(Subject::getTitle, name);
        subjectService.update(wrapper);
        return Result.success();
    }
}
